home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / x11 / jpeg / lib / jmemsys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-18  |  4.2 KB  |  159 lines

  1. /*
  2.  * jmemansi.c  (jmemsys.c)
  3.  *
  4.  * Copyright (C) 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file provides a simple generic implementation of the system-
  9.  * dependent portion of the JPEG memory manager.  This implementation
  10.  * assumes that you have the ANSI-standard library routine tmpfile().
  11.  * Also, the problem of determining the amount of memory available
  12.  * is shoved onto the user.
  13.  */
  14.  
  15. #undef    STREAM_IMAGE
  16. #include "jinclude.h"
  17. #include "jmemsys.h"
  18.  
  19. #ifdef INCLUDES_ARE_ANSI
  20. #include <stdlib.h>        /* to declare malloc(), free() */
  21. #else
  22. extern void * malloc PP((size_t size));
  23. extern void free PP((void *ptr));
  24. #endif
  25.  
  26. #ifndef SEEK_SET        /* pre-ANSI systems may not define this; */
  27. #define SEEK_SET  0        /* if not, assume 0 is correct */
  28. #endif
  29.  
  30.  
  31. static external_methods_ptr methods; /* saved for access to error_exit */
  32.  
  33. static long total_used;        /* total memory requested so far */
  34.  
  35.  
  36. /*
  37.  * Memory allocation and freeing are controlled by the regular library
  38.  * routines malloc() and free().
  39.  */
  40.  
  41. GLOBAL void *
  42. jget_small (size_t sizeofobject)
  43. {
  44.   total_used += sizeofobject;
  45.   return (void *) malloc(sizeofobject);
  46. }
  47.  
  48. GLOBAL void
  49. jfree_small (void * object)
  50. {
  51.   free(object);
  52. }
  53.  
  54. /*
  55.  * We assume NEED_FAR_POINTERS is not defined and so the separate entry points
  56.  * jget_large, jfree_large are not needed.
  57.  */
  58.  
  59.  
  60. /*
  61.  * This routine computes the total memory space available for allocation.
  62.  * It's impossible to do this in a portable way; our current solution is
  63.  * to make the user tell us (with a default value set at compile time).
  64.  * If you can actually get the available space, it's a good idea to subtract
  65.  * a slop factor of 5% or so.
  66.  */
  67.  
  68. #ifndef DEFAULT_MAX_MEM        /* so can override from makefile */
  69. #define DEFAULT_MAX_MEM        1000000L /* default: one megabyte */
  70. #endif
  71.  
  72. GLOBAL long
  73. jmem_available (long min_bytes_needed, long max_bytes_needed)
  74. {
  75.   return methods->max_memory_to_use - total_used;
  76. }
  77.  
  78.  
  79. /*
  80.  * Backing store (temporary file) management.
  81.  * Backing store objects are only used when the value returned by
  82.  * jmem_available is less than the total space needed.  You can dispense
  83.  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  84.  */
  85.  
  86.  
  87. METHODDEF void
  88. read_backing_store (backing_store_ptr info, void FAR * buffer_address,
  89.             long file_offset, long byte_count)
  90. {
  91.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  92.     ERREXIT(methods, "fseek failed on temporary file");
  93.   if (JFREAD(info->temp_file, buffer_address, byte_count)
  94.       != (size_t) byte_count)
  95.     ERREXIT(methods, "fread failed on temporary file");
  96. }
  97.  
  98.  
  99. METHODDEF void
  100. write_backing_store (backing_store_ptr info, void FAR * buffer_address,
  101.              long file_offset, long byte_count)
  102. {
  103.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  104.     ERREXIT(methods, "fseek failed on temporary file");
  105.   if (JFWRITE(info->temp_file, buffer_address, byte_count)
  106.       != (size_t) byte_count)
  107.     ERREXIT(methods, "fwrite failed on temporary file --- out of disk space?");
  108. }
  109.  
  110.  
  111. METHODDEF void
  112. close_backing_store (backing_store_ptr info)
  113. {
  114.   fclose(info->temp_file);
  115.   /* Since this implementation uses tmpfile() to create the file,
  116.    * no explicit file deletion is needed.
  117.    */
  118. }
  119.  
  120.  
  121. /*
  122.  * Initial opening of a backing-store object.
  123.  *
  124.  * This version uses tmpfile(), which constructs a suitable file name
  125.  * behind the scenes.  We don't have to use temp_name[] at all;
  126.  * indeed, we can't even find out the actual name of the temp file.
  127.  */
  128.  
  129. GLOBAL void
  130. jopen_backing_store (backing_store_ptr info, long total_bytes_needed)
  131. {
  132.   if ((info->temp_file = tmpfile()) == NULL)
  133.     ERREXIT(methods, "Failed to create temporary file");
  134.   info->read_backing_store = read_backing_store;
  135.   info->write_backing_store = write_backing_store;
  136.   info->close_backing_store = close_backing_store;
  137. }
  138.  
  139.  
  140. /*
  141.  * These routines take care of any system-dependent initialization and
  142.  * cleanup required.  Keep in mind that jmem_term may be called more than
  143.  * once.
  144.  */
  145.  
  146. GLOBAL void
  147. jmem_init (external_methods_ptr emethods)
  148. {
  149.   methods = emethods;        /* save struct addr for error exit access */
  150.   emethods->max_memory_to_use = DEFAULT_MAX_MEM;
  151.   total_used = 0;
  152. }
  153.  
  154. GLOBAL void
  155. jmem_term (void)
  156. {
  157.   /* no work */
  158. }
  159.